/* Copyright (c) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package teste;

import java.io.IOException;
import java.util.Collection;

import org.opensocial.client.OpenSocialClient;
import org.opensocial.client.OpenSocialRequestException;
import org.opensocial.data.OpenSocialField;
import org.opensocial.data.OpenSocialPerson;

public class DisplayFriends {

	public static void main(String[] args) {
		try {
			OpenSocialClient openSocialClient = new OpenSocialClient(
					"orkut.com");

			// orkut supports both the REST and RPC protocols; RPC is preferred
			// because
			// RPC supports batch requests
			openSocialClient.setProperty(
					OpenSocialClient.Property.RPC_ENDPOINT,
					"http://sandbox.orkut.com/social/rpc");

			// Credentials provided here are associated with the gadget located
			// at
			// http://opensocial-resources.googlecode.com/svn/samples/rest_rpc/sample.xml;
			// If you install this gadget, you can substitute your own
			// OpenSocial ID
			// for the one used below and fetch your profile data and friends
			openSocialClient.setProperty(
					OpenSocialClient.Property.CONSUMER_SECRET,
					"uynAeXiWTisflWX99KU1D2q5");
			openSocialClient.setProperty(
					OpenSocialClient.Property.CONSUMER_KEY,
					"orkut.com:623061448914");
			openSocialClient.setProperty(OpenSocialClient.Property.VIEWER_ID,
					"10886451842774632785");

			// Retrieve the friends of the specified user using the
			// OpenSocialClient
			// method fetchFriends
			Collection<OpenSocialPerson> friends = openSocialClient
					.fetchFriends();

			System.out.println("----------");

			String nickName;
			try {
				nickName = openSocialClient.fetchPerson().getDisplayName();
				System.out.println("Eu: " + nickName);
			} catch (OpenSocialRequestException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			// The fetchFriends method returns a typical Java Collection object
			// with
			// all of the methods you're already accustomed to like size()
			System.out.println(friends.size() + " friends:");

			for (int i = 0; i < openSocialClient.fetchPerson().fieldNames().length; i++) {
				System.out.println(openSocialClient.fetchPerson().fieldNames()[i]);
			}
			
			// Iterate through the Collection
			for (OpenSocialPerson friend : friends) {
				// Output the name of the current friend
				System.out.println("DisplayName: " + friend.getDisplayName());
				System.out.println("Id: " + friend.getId());

				System.out.println("\n");

				OpenSocialField thumbnailUrlField = friend
						.getField("thumbnailUrl");
				if (!thumbnailUrlField.isComplex()) {
					System.out.println("Thumbnail URL: "
							+ thumbnailUrlField.getStringValue());
				}
			}

			System.out.println("----------");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
